package com.hafu.swing;

import com.hafu.model.Teacher;
import com.hafu.service.TeacherService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// 编辑教师对话框
public class EditTeacherDialog extends JDialog {
    private TeacherService teacherService;
    private Teacher teacher;
    private JTextField nameField;
    private JComboBox<String> genderComboBox;
    private JTextField ageField;
    private JTextField titleField;
    private JTextField departmentField;
    private JTextField phoneField;

    public EditTeacherDialog(JPanel parentPanel, TeacherService teacherService, String teacherId) {
        super((JFrame) SwingUtilities.getWindowAncestor(parentPanel), "编辑教师", true);
        this.teacherService = teacherService;
        this.teacher = teacherService.getTeacherById(teacherId);

        if (this.teacher == null) {
            JOptionPane.showMessageDialog(this, "未找到该教师信息", "错误", JOptionPane.ERROR_MESSAGE);
            dispose();
            return;
        }

        setSize(400, 450);
        setLocationRelativeTo(parentPanel);
        setLayout(new BorderLayout());

        initComponents();
    }

    private void initComponents() {
        // 表单面板
        JPanel formPanel = new JPanel(new GridLayout(7, 2, 10, 10));
        formPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        formPanel.add(new JLabel("教师工号:"));
        JTextField teacherIdField = new JTextField(teacher.getTeacherId());
        teacherIdField.setEditable(false); // 工号不可编辑
        formPanel.add(teacherIdField);

        formPanel.add(new JLabel("姓名:"));
        nameField = new JTextField(teacher.getName());
        formPanel.add(nameField);

        formPanel.add(new JLabel("性别:"));
        genderComboBox = new JComboBox<>(new String[]{"男", "女"});
        if (teacher.getGender() != null) {
            genderComboBox.setSelectedItem(teacher.getGender());
        }
        formPanel.add(genderComboBox);

        formPanel.add(new JLabel("年龄:"));
        ageField = new JTextField(teacher.getAge() != null ? teacher.getAge().toString() : "");
        formPanel.add(ageField);

        formPanel.add(new JLabel("职称:"));
        titleField = new JTextField(teacher.getTitle());
        formPanel.add(titleField);

        formPanel.add(new JLabel("所属院系:"));
        departmentField = new JTextField(teacher.getDepartment());
        formPanel.add(departmentField);

        formPanel.add(new JLabel("联系电话:"));
        phoneField = new JTextField(teacher.getPhone());
        formPanel.add(phoneField);

        // 按钮面板
        JPanel buttonPanel = new JPanel();
        JButton saveButton = new JButton("保存");
        JButton cancelButton = new JButton("取消");

        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateTeacher();
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);

        add(formPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void updateTeacher() {
        // 验证输入
        String name = nameField.getText().trim();
        String gender = (String) genderComboBox.getSelectedItem();
        String ageStr = ageField.getText().trim();
        String title = titleField.getText().trim();
        String department = departmentField.getText().trim();
        String phone = phoneField.getText().trim();

        if (name.isEmpty()) {
            JOptionPane.showMessageDialog(this, "姓名不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        Integer age = null;
        try {
            if (!ageStr.isEmpty()) {
                age = Integer.parseInt(ageStr);
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "年龄必须是数字", "输入错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        // 更新教师对象
        teacher.setName(name);
        teacher.setGender(gender);
        teacher.setAge(age);
        teacher.setTitle(title);
        teacher.setDepartment(department);
        teacher.setPhone(phone);

        // 更新教师信息
        boolean success = updateTeacherInDatabase(teacher);
        if (success) {
            JOptionPane.showMessageDialog(this, "教师信息更新成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, "教师信息更新失败", "失败", JOptionPane.ERROR_MESSAGE);
        }
    }

    private boolean updateTeacherInDatabase(Teacher teacher) {
        // 这里应该调用 DAO 层更新教师信息
        // 示例实现
        return true; // 假设更新成功
    }
}